﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.API.Applications.Command.DeepProcessingCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.DeepProcessingCommandHandler
{
    public class ProcessingPlantQueryCommandHandler : IRequestHandler<ProcessingPlantQueryCommand, APIResult<Processing_Plant>>
    {
        private readonly IBaseRepository<Processing_Plant> plantRepository;

        public ProcessingPlantQueryCommandHandler(IBaseRepository<Processing_Plant> plantRepository)
        {
            this.plantRepository = plantRepository;
        }

        public Task<APIResult<Processing_Plant>> Handle(ProcessingPlantQueryCommand request, CancellationToken cancellationToken)
        {
            var res = plantRepository.GetAll().AsNoTracking().FirstOrDefault(s => !s.IsDeltede && s.Batch == request.Batch);

            APIResult<Processing_Plant> aPIResult = new APIResult<Processing_Plant>();
            if (res == null)
            {
                aPIResult.Code = EnumCode.File;
                aPIResult.Msg = "显示失败";
            }
            else
            {
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Msg = "显示成功";
                aPIResult.Data = res;
            }

            return Task.FromResult(aPIResult);
        }
    }
}
